package renguanyu.app.tools.util;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import com.mysql.cj.jdbc.MysqlDataSource;

public class DbUtil {

	private static MysqlDataSource dataSource;
	private static QueryRunner queryRunner;

	static {
		dataSource = new MysqlDataSource();
		dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/mydb");
		dataSource.setUser("root");
		dataSource.setPassword("123456");
		queryRunner = new QueryRunner(dataSource);
	}

	public static QueryRunner getQueryRunner() {
		return queryRunner;
	}

	public static MysqlDataSource getDataSource() {
		return dataSource;
	}
	
	public static void update(String sql, Object... params) {
		try {
			queryRunner.update(sql, params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static <T> T query(String sql,ResultSetHandler<T> rsh){
		try {
			T query = queryRunner.query(sql, rsh);
			return query;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
